﻿var CurrentAd = {};
var CurrentType = 0;

Ext.onReady(function () {
    PageSafe();

    var adstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('Advertisement.AdvertisementView', 'GetMyAdvertisement'),
        root: 'ResponseObject',
        fields: ['PID', 'GName', 'GUrl', 'GRemark', 'Creater', 'Period', 'NTime', 'SurplusTime', 'GT', 'ChargeUser', 'ChargeUser.UserName', 'TransfS'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                re.params.info = {
                    AdName: Ext.getCmp("SearchName").getValue(),
                    SearchType: CurrentType
                };
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var adsm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = adgrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentAd = k[0].json;
                }
            },
            rowdeselect: function () {
                CurrentAd = {};
            }
        }
    });
    function NumFormat(s, m) {
        m.css = 'RedNum';
        return s;
    }
    var adgrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        height: 500,
        sm: adsm,
        loadMask: true, //加载
        columns: [
       adsm,
       { id: 'GName', header: '广告名称', width: 130, dataIndex: 'GName', sortable: true },
       { id: 'GUrl', header: '广告路径', width: 200, dataIndex: 'GUrl', sortable: true,
		renderer:function(v){
			return '<a href="#" onClick="viewPage(\'' + v + '\')">' + v + '</a>';
		}},
       { id: 'ChargeUser.UserName', header: '发布人', width: 100, dataIndex: 'ChargeUser.UserName', sortable: true },
       { id: 'GRemark', header: '备注', width: 260, dataIndex: 'GRemark', sortable: true },
       { id: 'Period', header: '有效日期(天)', width: 100, dataIndex: 'Period', sortable: true },
       { id: 'GT', header: '来源', width: 100, dataIndex: 'GT', sortable: true },
        { id: 'TransfS', header: '状态', width: 100, dataIndex: 'TransfS', sortable: true },
       { id: 'SurplusTime', header: '剩余日期(天)', width: 100, dataIndex: 'SurplusTime', sortable: true, renderer: NumFormat },
       { id: 'NTime', header: '日期', width: 100, dataIndex: 'NTime', sortable: true }
   ],
        store: adstore
    });


    var panelwin = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'center',
            title: '广告基本管理',
            tbar: [{ text: '上传广告', iconCls: 'upload-icon', handler: function () {
                var upwin = new Ext.Window({
                    width: 400,
                    resizable: false,
                    modal: true,
                    height: 200,
                    title: '广告上传',
                    bodyStyle: 'background:#ffffff',
                    items: new Ext.form.FormPanel({
                        iconCls: 'Advertisement-icon',
                        enctype: 'multipart/form-data',
                        method: 'POST',
                        border: false,
                        bodyStyle: 'padding-top:20px',
                        fileUpload: true,
                        id: 'ad_main',
                        defaults: { xtype: 'textfield' },
                        labelAlign: 'right',
                        items: [{
                            fieldLabel: '广告名称',
                            width: 160,
                            id: 'Ad_Name',
                            name: 'Ad_Name'
                        }, {
                            fieldLabel: '广告备注',
                            width: 230,
                            id: 'Ad_Remark',
                            name: 'Ad_Remark'
                        }, {
                            fieldLabel: '广告有效期',
                            width: 100,
                            id: 'Ad_Period',
                            name: 'Ad_Period'
                        }, {
                            fieldLabel: '广告上传',
                            width: 200,
                            id: 'Ad_Upload',
                            name: 'Ad_Upload',
                            inputType: 'file'
                        }]
                    }),
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        if (Ext.getCmp("Ad_Name").getValue() == "") { Ext.Msg.alert("检查提示", "请输入广告名称"); return; }
                        if (Ext.getCmp("Ad_Upload").getValue() == "") { Ext.Msg.alert("检查提示", "请上传广告"); return; }
                        Ext.getCmp("ad_main").getForm().submit({
                            url: 'GGFile.aspx',
                            failure: function (form, action) { Ext.Msg.alert("提示", "上传广告失败"); },
                            success: function (form, action) {
                                upwin.close();
                                adstore.load();
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { upwin.close(); } }]
                }).show();
            }
            }, { text: '删除广告', iconCls: 'del-icon', handler: function () {
                if (CurrentAd.PID == null) { Ext.Msg.alert("提示信息", "请选择一份广告进行删除"); return; }
                var re = new JsonRequest();
                re.params.gg = CurrentAd;

                Ext.Ajax.request({
                    url: Ext.getClassUrl('Advertisement.AdvertisementView', 'DelUpLoadAd'),
                    jsonData: re,
                    success: function (re, op) {
                        var obj = Ext.decode(re.responseText);
                        if (obj.ResponseObject != null) {
                            Ext.Msg.alert("操作提示", "广告删除成功");
                            adstore.load();
                        }
                        else {
                            Ext.Msg.alert("操作提示", "广告删除失败，请联系管理员");
                        }
                    }
                });
            }
            }, '-', { text: '定制广告', iconCls: 'custome-icon', handler: function () {
                Ext.Msg.alert("友情提示", "功能尚未开放");
            }
            }, { text: '修改广告', iconCls: 'update-icon', handler: function () {
                Ext.Msg.alert("友情提示", "功能尚未开放");
            }
            }, { text: '删除广告', iconCls: 'del-icon', handler: function () {
                Ext.Msg.alert("友情提示", "功能尚未开放");
            }
            }, '-', '名称查询：', { xtype: 'textfield', width: 100, id: 'SearchName', name: 'SearchName' }, { text: '查询', iconCls: 'search-icon', handler: function () {
                adstore.load();
            }
            }],
            items: adgrid
        }],
        listeners: {
            render: function () {
                adstore.load();
            }
        }
    });
});

function viewPage(path) {
	var f = new Ext.form.TextField({
		fieldLabel: '目标地址',
		anchor:'-20',
		value:'www.baidu.com'
	});

	var upwin = new Ext.Window({
		width: 400,
		resizable: false,
		modal: true,
		height: 150,
		title: '广告预览',
		bodyStyle: 'background:#ffffff',
		items: new Ext.form.FormPanel({
			iconCls: 'Advertisement-icon',
			border: false,
			bodyStyle: 'padding-top:20px',
			defaults: { xtype: 'textfield' },
			labelAlign: 'right',
			items: f
		}),
		buttons: [{ 
			text: '确定', 
			iconCls: 'save-icon', 
			handler: function () {
				var t = f.getValue();
				window.open('/WebUI/GatewayPage.aspx?' + Ext.urlEncode({
					target:t,
					path:path
				}));
			}	
		}, { 
			text: '取消', 
			iconCls: 'cancel-icon', 
			handler: function () { 
				upwin.close(); 
			} 
		}]
	}).show();
}